﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ETL.Entity;
namespace ETL.DataAccess
{
    public class ProjectETRRepository
    {
        //Hardcoded condition !
        public IEnumerable<project_etr> GetProjectETR()

        {
            var db = new ETLDataEntities();
            var result = from s in db.project_etr
                         where s.etr_closed == null && s.etr_ref != "ETR 10-99" && s.etr_active == 1
                         select s;
            return result;
        }

        public project_etr GetProjectETRbyRef(String etrRef)
        {
            project_etr result = null;
            if (etrRef != String.Empty)
            {
                var db = new ETLDataEntities();
                var etr = from s in db.project_etr
                          where s.etr_ref == etrRef
                          select s;
                if (etr.Count() > 0)
                {
                    return etr.First();
                }
            }
            return result;
        }

        public void Update(project_etr etr)
        {
            var db = new ETLDataEntities();
            project_etr etrToUpdate = (from s in db.project_etr where s.etr_ref == etr.etr_ref select s).First();

            etrToUpdate.etr_active = etr.etr_active;
            etrToUpdate.etr_authorised_by = etr.etr_authorised_by;
            etrToUpdate.etr_cad = etr.etr_cad;
            etrToUpdate.etr_cad_hours = etr.etr_cad_hours;
            etrToUpdate.etr_closed = etr.etr_closed;
            etrToUpdate.etr_closed_by = etr.etr_closed_by;
            etrToUpdate.etr_created = etr.etr_created;
            etrToUpdate.etr_date_authorised = etr.etr_date_authorised;
            etrToUpdate.etr_date_requested = etr.etr_date_requested;
            etrToUpdate.etr_date_required = etr.etr_date_required;
            etrToUpdate.etr_department = etr.etr_department;
            etrToUpdate.etr_description = etr.etr_description;
            etrToUpdate.etr_electronics = etr.etr_electronics;
            etrToUpdate.etr_electronics_hours = etr.etr_electronics_hours;
            etrToUpdate.etr_email_sent = etr.etr_email_sent;
            etrToUpdate.etr_originator = etr.etr_originator;
            etrToUpdate.etr_other = etr.etr_other;
            etrToUpdate.etr_other_hours = etr.etr_other_hours;
            etrToUpdate.etr_others = etr.etr_others;
            etrToUpdate.etr_overhead = etr.etr_overhead;
            etrToUpdate.etr_percent_complete = etr.etr_percent_complete;
            etrToUpdate.etr_rd = etr.etr_rd;
            etrToUpdate.etr_reason = etr.etr_reason;
            etrToUpdate.etr_rejected_by = etr.etr_rejected_by;
            etrToUpdate.etr_rejection_reason = etr.etr_rejection_reason;
            etrToUpdate.etr_requested_by = etr.etr_requested_by;
            etrToUpdate.etr_requirements = etr.etr_requirements;
            etrToUpdate.etr_sheets = etr.etr_sheets;
            etrToUpdate.etr_software = etr.etr_software;
            etrToUpdate.etr_software_hours = etr.etr_software_hours;
            etrToUpdate.etr_title = etr.etr_title;


            db.Entry(etrToUpdate).State = EntityState.Modified;

            db.SaveChanges();

        }

    }
}
